import plugin from '../../lib/plugins/plugin.js';
import { segment } from 'oicq';
import axios from 'axios';
import moment from'moment';
//使用前请在云崽根目录输入 pnpm add -w axios

export class SRQueryTime extends plugin {
    constructor() {
        super({
            name: '查询SR时间',
            dsc: '查询SR日程时间及倒计时',
            event:'message',
            priority: -100088888080,
            rule: [
                {
                    reg: '^#星铁(查询)?(时间|日程|日程表|倒计时)$',
                    fnc: 'SRquerySchedule'
                }
            ]
        });
    }

    async SRquerySchedule(e) {
        try {
            const response = await axios.get('https://gitee.com/euiko/Panel/raw/img/SRtime.txt');
            const lines = response.data.split('\n');
            const now = moment();
            let result = '崩坏:星穹铁道-日程表\n-\n';
            for (const line of lines) {
                const parts = line.split(' ');
                if (parts.length === 2) {
                    const scheduleTime = moment(parts[1], 'YYYY-MM-DD');
                    const diff = scheduleTime.diff(now);
                    let countdown = '';
                    if (diff <= 0) {
                        const endOfDay = moment(parts[1], 'YYYY-MM-DD').endOf('day');
                        if (endOfDay.diff(now) >= 0) {
                            countdown = '进行中';
                        } else {
                            continue;
                        }
                    } else {
                        const diffDays = Math.floor(diff / (1000 * 60 * 60 * 24));
                        const diffHours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                        const diffMinutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
                        const diffSeconds = Math.floor((diff % (1000 * 60)) / 1000);

                        if (diffDays > 0) {
                            countdown = `${diffDays}天`;
                        } else if (diffHours > 0) {
                            countdown = `${diffHours}时`;
                        } else if (diffMinutes > 0) {
                            countdown = `${diffMinutes}分`;
                        } else {
                            countdown = `${diffSeconds}秒`;
                        }
                    }
                    result += `${parts[0]} ${parts[1]} ${countdown}\n`;
                }
            }
            if (result === '全部日程时间及倒计时：\n') {
                result += '暂无即将开始的日程';
            }
            await this.reply(result);
        } catch (error) {
            console.error('获取日程信息失败:', error);
            await this.reply('获取日程信息失败，请稍后再试。');
        }
    }
}
